package com.ruoyi.user.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.user.domain.RegionUserWishlists;

import com.ruoyi.user.service.IRegionUserWishlistsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 区域用户愿望单Controller
 *
 * @author ruoyi
 * @date 2025-10-21
 */
@RestController
@RequestMapping("/user/wishlists")
public class RegionUserWishlistsController extends BaseController
{
    @Autowired
    private IRegionUserWishlistsService regionUserWishlistsService;

    /**
     * 查询区域用户愿望单列表
     */
    @PreAuthorize("@ss.hasPermi('user:wishlists:list')")
    @GetMapping("/list")
    public TableDataInfo list(RegionUserWishlists regionUserWishlists)
    {
        startPage();
        List<RegionUserWishlists> list = regionUserWishlistsService.selectRegionUserWishlistsList(regionUserWishlists);
        return getDataTable(list);
    }

    /**
     * 导出区域用户愿望单列表
     */
    @PreAuthorize("@ss.hasPermi('user:wishlists:export')")
    @Log(title = "区域用户愿望单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RegionUserWishlists regionUserWishlists)
    {
        List<RegionUserWishlists> list = regionUserWishlistsService.selectRegionUserWishlistsList(regionUserWishlists);
        ExcelUtil<RegionUserWishlists> util = new ExcelUtil<RegionUserWishlists>(RegionUserWishlists.class);
        util.exportExcel(response, list, "区域用户愿望单数据");
    }

    /**
     * 获取区域用户愿望单详细信息
     */
    @PreAuthorize("@ss.hasPermi('user:wishlists:query')")
    @GetMapping(value = "/{wishlistId}")
    public AjaxResult getInfo(@PathVariable("wishlistId") String wishlistId)
    {
        return success(regionUserWishlistsService.selectRegionUserWishlistsByWishlistId(wishlistId));
    }

    /**
     * 新增区域用户愿望单
     */
    @PreAuthorize("@ss.hasPermi('user:wishlists:add')")
    @Log(title = "区域用户愿望单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RegionUserWishlists regionUserWishlists)
    {
        return toAjax(regionUserWishlistsService.insertRegionUserWishlists(regionUserWishlists));
    }

    /**
     * 修改区域用户愿望单
     */
    @PreAuthorize("@ss.hasPermi('user:wishlists:edit')")
    @Log(title = "区域用户愿望单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RegionUserWishlists regionUserWishlists)
    {
        return toAjax(regionUserWishlistsService.updateRegionUserWishlists(regionUserWishlists));
    }

    /**
     * 删除区域用户愿望单
     */
    @PreAuthorize("@ss.hasPermi('user:wishlists:remove')")
    @Log(title = "区域用户愿望单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{wishlistIds}")
    public AjaxResult remove(@PathVariable String[] wishlistIds)
    {
        return toAjax(regionUserWishlistsService.deleteRegionUserWishlistsByWishlistIds(wishlistIds));
    }

    /**
     * 根据区域ID查询愿望榜单列表
     */
    @PreAuthorize("@ss.hasPermi('user:wishlists:list')")
    @GetMapping("/region/{regionId}")
    public AjaxResult getWishlistsByRegionId(@PathVariable("regionId") String regionId)
    {
        List<RegionUserWishlists> list = regionUserWishlistsService.selectRegionUserWishlistsByRegionId(regionId);
        return success(list);
    }

    /**
     * 根据区域ID和用户ID添加愿望
     */
    @PreAuthorize("@ss.hasPermi('user:wishlists:add')")
    @Log(title = "区域用户愿望单", businessType = BusinessType.INSERT)
    @PostMapping("/add/{regionId}/{customerId}")
    public AjaxResult addWishlistByRegionAndCustomer(
            @PathVariable("regionId") String regionId,
            @PathVariable("customerId") String customerId,
            @RequestBody RegionUserWishlists wishlistData)
    {
        try {
            int result = regionUserWishlistsService.addWishlistByRegionAndCustomer(
                    regionId, 
                    customerId, 
                    wishlistData
            );
            
            if (result > 0) {
                return success("愿望添加成功");
            } else {
                return error("愿望添加失败，可能已存在相同内容的愿望");
            }
        } catch (IllegalArgumentException e) {
            return error(e.getMessage());
        } catch (Exception e) {
            return error("愿望添加失败：" + e.getMessage());
        }
    }
}
